package thoven.achievement.net.ideapluginachievement.service

import com.intellij.notification.Notification
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import thoven.achievement.net.ideapluginachievement.model.Achievement

/**
 * 成就通知服务
 */
@Service(Service.Level.PROJECT)
class AchievementNotificationService(private val project: Project) {
    
    private val settingsService = SettingsService.getInstance()
    
    /**
     * 显示成就解锁通知（增强版）
     */
    fun showAchievementUnlockedNotification(achievement: Achievement) {
        // 检查通知设置
        if (!settingsService.isNotificationsEnabled()) {
            return
        }
        
        // 检查弹窗通知是否启用
        if (!settingsService.isPopupNotificationsEnabled()) {
            return
        }
        
        // 检查工作时间设置（如果启用了工作时间限制）
        if (settingsService.isWorkTimeReminderEnabled() && !settingsService.isWorkTime()) {
            // 非工作时间，可以选择降低通知强度或者不显示
            return
        }
        
        // 根据成就类型确定标题样式
        val (titlePrefix, rarity) = getAchievementStyle(achievement.type)
        
        // 简化标题，避免emoji显示问题
        val title = "成就解锁！"
        
        val content = buildString {
            append("<html><body>")
            append("<center>")
            append("<table width='100%' cellpadding='5' cellspacing='0' style='text-align: center;'>")
            append("<tr><td><b style='color: #FFD700; font-size: 16px;'>$titlePrefix ${achievement.getName()}</b></td></tr>")
            append("<tr><td><i style='color: #B0B0B0; font-size: 11px;'>${rarity}</i></td></tr>")
            append("<tr><td style='color: #FFFFFF; font-size: 13px; padding: 8px 0;'>${achievement.getDescription()}</td></tr>")
            append("<tr><td style='color: #90EE90; font-size: 12px;'><b>继续加油，解锁更多成就！</b></td></tr>")
            append("</table>")
            append("</center>")
            append("</body></html>")
        }
        
        showNotification(title, content, NotificationType.INFORMATION)
    }
    
    /**
     * 获取成就样式（标题前缀和稀有度）
     */
    private fun getAchievementStyle(type: thoven.achievement.net.ideapluginachievement.model.AchievementType): Pair<String, String> {
        return when (type) {
            // 里程碑成就 - 史诗级
            thoven.achievement.net.ideapluginachievement.model.AchievementType.LEGENDARY_1000,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.CODE_MACHINE,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.DREAM_100_DAYS -> 
                Pair("✨✨✨", "【史诗成就】")
            
            // 连续性成就 - 稀有级
            thoven.achievement.net.ideapluginachievement.model.AchievementType.CRAZY_7_DAYS,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.IRON_30_DAYS,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.HARD_WORKER -> 
                Pair("✨✨", "【稀有成就】")
            
            // 趣味彩蛋 - 特殊级
            thoven.achievement.net.ideapluginachievement.model.AchievementType.BUG_HARVESTER,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.CTRL_S_BELIEVER,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.COPY_PASTE_MASTER,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.IDEA_RESTARTER,
            thoven.achievement.net.ideapluginachievement.model.AchievementType.CONSOLE_LOG_PRIEST -> 
                Pair("🎪", "【趣味彩蛋】")
            
            // 普通成就
            else -> Pair("🏆", "【成就达成】")
        }
    }
    
    /**
     * 显示信息通知
     */
    fun showInfoNotification(title: String, content: String) {
        showNotification(title, content, NotificationType.INFORMATION)
    }
    
    /**
     * 显示警告通知
     */
    fun showWarningNotification(title: String, content: String) {
        showNotification(title, content, NotificationType.WARNING)
    }
    
    /**
     * 显示错误通知
     */
    fun showErrorNotification(title: String, content: String) {
        showNotification(title, content, NotificationType.ERROR)
    }
    
    /**
     * 显示通知
     */
    private fun showNotification(title: String, content: String, type: NotificationType) {
        try {
            val notificationGroup = NotificationGroupManager.getInstance()
                .getNotificationGroup("Achievement Notifications")
            
            val notification = notificationGroup.createNotification(
                title,
                content,
                type
            )
            
            // 显示通知
            Notifications.Bus.notify(notification, project)
        } catch (e: Exception) {
            // 如果通知组不存在，使用默认方式
            val notification = Notification(
                "Achievement Notifications",
                title,
                content,
                type
            )
            Notifications.Bus.notify(notification, project)
        }
    }
    
    /**
     * 显示每日统计通知
     */
    fun showDailyStatsNotification(addedLines: Int, deletedLines: Int, netLines: Int) {
        val title = "📊 今日代码统计"
        val content = """
            新增: <b>$addedLines</b> 行 | 
            删除: <b>$deletedLines</b> 行 | 
            净增: <b>$netLines</b> 行
        """.trimIndent()
        
        showNotification(title, content, NotificationType.INFORMATION)
    }
    
    /**
     * 显示连续编码天数通知
     */
    fun showConsecutiveDaysNotification(days: Int) {
        val title = "🔥 连续编码"
        val content = "你已经连续编码 <b>$days</b> 天了！继续保持！"
        
        showNotification(title, content, NotificationType.INFORMATION)
    }
    
    companion object {
        fun getInstance(project: Project): AchievementNotificationService = project.service()
    }
} 
